package com.beiing.tianshuai.tianshuai.mine.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.MyCollectionAdapter;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.HuoDongItemBean;
import com.beiing.tianshuai.tianshuai.entity.MyCollectionBean;
import com.beiing.tianshuai.tianshuai.huodong.ActivitiesDetailActivity;
import com.beiing.tianshuai.tianshuai.mine.presenter.MyCollectionPresenter;
import com.beiing.tianshuai.tianshuai.mine.view.MyCollectionViewImpl;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.widget.CustomDialog;
import com.beiing.tianshuai.tianshuai.widget.MyRefreshHeader;
import com.beiing.tianshuai.tianshuai.widget.RecycleViewDivider;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyCollectionActivity extends BaseActivity implements MyCollectionViewImpl {

    private static final String                         TAG = "MyCollectionActivity";
    private Unbinder                                    mUnbinder;
    private Context                                     mContext;
    private MyCollectionPresenter                       mPresenter;
    private List<MyCollectionBean.DataBean.NumberBean>  mAttentionList;
    private MyCollectionAdapter                         mAdapter;
    private int                                         mPosition;
    private String                                      mUid;

    @BindView(R.id.toolbar_navigation)
    ImageView mToolbarNavigation;
    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;
    @BindView(R.id.rv_my_collection)
    RecyclerView mRvMyCollection;
    @BindView(R.id.srl_my_refresh_layout)
    SmartRefreshLayout mSrlMyRefreshLayout;
    @BindView(R.id.txt_empty_warning)
    TextView mEmptyWarning;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_collection;
    }

    @Override
    protected void init() {
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        Intent intent = getIntent();
        mUid = intent.getStringExtra("uid");
        initToolbar();
        initRefreshLayout();
        initRecyclerView();
        initPresenter();
        initListener();
    }

    private void initToolbar() {
        mToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initRefreshLayout() {
        mSrlMyRefreshLayout.setRefreshHeader(new MyRefreshHeader(mContext));
        mSrlMyRefreshLayout.setEnableLoadmore(false);
        mSrlMyRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.getCollectionResult(mUid);
            }
        });
    }

    private void initRecyclerView() {
        mAttentionList = new ArrayList<>();
        mAdapter = new MyCollectionAdapter(mContext, mAttentionList, mUid);
        mRvMyCollection.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mRvMyCollection.setNestedScrollingEnabled(false);
        mRvMyCollection.setLayoutManager(linearLayoutManager);
        //绘制item间的分割线，
        mRvMyCollection.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, 1, ContextCompat.getColor(mContext, R.color.textColorGrayDark)));
    }

    private void initPresenter() {
        mPresenter = new MyCollectionPresenter(this);
        mPresenter.getCollectionResult(mUid);
    }

    private void initListener() {
        mAdapter.setListener(new MyCollectionAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(int position, MyCollectionBean.DataBean.NumberBean bean) {
                HuoDongItemBean.DataBean.ActiveBean activeBean = new HuoDongItemBean.DataBean.ActiveBean();
                activeBean.setId(bean.getId());
                activeBean.setUid(bean.getUid());
                activeBean.setActionTime(bean.getActionTime());
                activeBean.setExitTime(bean.getExitTime());
                activeBean.setStrech(bean.getStrech());
                activeBean.setCover(bean.getCover());
                activeBean.setSponsor(bean.getSponsor());
                activeBean.setPhoto(bean.getPhoto());
                activeBean.setLessons(bean.getLessons());
                activeBean.setLocation(bean.getLocation());
                activeBean.setCount("0");
                activeBean.setMuch("0");
                Intent intent = new Intent(mContext, ActivitiesDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("active", activeBean);
                intent.putExtras(bundle);
                intent.putExtra("active_id", activeBean.getId());
                startActivity(intent);
            }

            @Override
            public void onDelItemListener(final int position, final MyCollectionBean.DataBean.NumberBean bean) {
                CustomDialog dialog = new CustomDialog(mContext, R.style.dialog, "确定取消收藏这个活动吗？", new CustomDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm) {
                        if (confirm) {
                            mPresenter.getDelResult(mUid, bean.getId());
                            mPosition = position;
                            dialog.dismiss();
                        } else {
                            dialog.dismiss();
                        }
                    }
                }).setTitle("提示");
                dialog.setCanceledOnTouchOutside(true);
                dialog.show();
            }
        });
    }

    @Override
    public void showProgress() {
    }

    @Override
    public void hideProgress() {
    }

    @Override
    public void onRequestSuccess(MyCollectionBean bean) {
        if (mSrlMyRefreshLayout.isRefreshing()) {
            mSrlMyRefreshLayout.finishRefresh();
        }
        List<MyCollectionBean.DataBean.NumberBean> number = bean.getData().getNumber();
        if (number.size() > 0) {
            mEmptyWarning.setVisibility(View.GONE);
            mAttentionList.clear();
            mAttentionList.addAll(number);
            mAdapter.notifyDataSetChanged();
            LogUtils.i("onRequestSuccess: " + bean.getData().toString(), TAG);
        } else {
            mEmptyWarning.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        if (mSrlMyRefreshLayout.isRefreshing()) {
            mSrlMyRefreshLayout.finishRefresh();
        }
        showShortText("网络异常");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }

    @Override
    public void onDelResult(CommonBean bean) {
        if (bean.getCode() == 200) {
            mAttentionList.remove(mPosition);
            mAdapter.notifyItemRemoved(mPosition);
            mPresenter.getCollectionResult(mUid);
        } else {
            showShortText("删除失败");
        }
    }
}
